package tbs.framework.sql.order.impl;

import tbs.framework.sql.enums.QueryOrderEnum;
import tbs.framework.sql.order.IQueryOrderedModel;
import tbs.framework.sql.order.IQueryOrderBy;

import java.lang.reflect.Field;

/**
 * @author tongj
 */
public class OrderedModelOrderImpl implements IQueryOrderBy {
    @Override
    public QueryOrderEnum orderBy(Object target, Field field, String mappedName) {
        QueryOrderEnum result = null;
        if (IQueryOrderedModel.class.isAssignableFrom(field.getType())) {
            field.setAccessible(true);
            try {
                IQueryOrderedModel orderedModel = (IQueryOrderedModel)field.get(target);
                result = orderedModel.getOrdered();
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                field.setAccessible(false);
            }
        } else {
            throw new UnsupportedOperationException("only support for IOrderedModel");
        }

        return result;
    }
}
